import Vue from 'vue'

/** ========================================路径规划功能=================================================== */
let pathPlanGeojson = {
  type: 'FeatureCollection',
  features: []
}

const pathPlanGuidGeoJson = {
  type: 'FeatureCollection',
  features: []
}

let pathPlanPoints = []

const pathPlanUrl = 'http://218.10.70.76:8091/iserver/services/transportationAnalyst-trafficNet/rest/networkanalyst/RoadNetTest_Network@RoadNetTest'

let startPoint = null

let endPoint = null

let pointType = -1

let startPointMarker = null

let endPointMarker = null

const markers = []

class PathPlan {
  /**
   *pathPlan() 路径规划分析
   * @static
   * @memberof PathPlan
   */
  static pathPlan() {
    if (startPoint == null || endPoint == null) {
      alert('请输入起点或终点')
    } else {
      let nodes = []
      nodes.push(startPoint)
      nodes = nodes.concat(pathPlanPoints)
      nodes.push(endPoint)
      const resultSetting = new SuperMap.TransportationAnalystResultSetting({
        // 是否在分析结果中包含弧段要素集合
        returnEdgeFeatures: true,
        // 返回的弧段要素集合中是否包含集合对象信息
        returnEdgeGeometry: true,
        // 返回的结果中是否包含经过弧段ID集合
        returnEdgeIDs: true,
        // 返回的分析结果总是否包含结点要素集合
        returnNodeFeatures: true,
        // 返回的结点要素集合中是否包含集合对象信息
        returnNodeGeometry: true,
        // 返回的分析结果中是否包含经过结点ID集合
        returnNodeIDs: true,
        // 返回分析结果中是否包含行驶导引集合
        returnPathGuides: true,
        // 返回结果中是否包含路由对象集合
        returnRoutes: true
      })

      // 网络分析通用参数
      const analystParameter = new SuperMap.TransportationAnalystParameter({
        // 分析结果返回的内容
        resultSetting: resultSetting

        // 阻力字段的名称
        // weightFieldName: "length"
      })

      // 最佳路径分析参数
      const findPathParams = new SuperMap.FindPathParameters({
        // 是否通过结点ID指定路径分析的结点
        isAnalyzeById: false,
        // 最佳路径分析经过的结点或设施点数组
        nodes: nodes,
        // 是否按照弧段数最少的进行最佳路径分析
        hasLeastEdgeCount: false,
        // 交通网络分析通用参数
        parameter: analystParameter
      })

      const findPathService = new mapboxgl.supermap.NetworkAnalystService(
        pathPlanUrl
      )

      return new Promise((resolve, reject) => {
        findPathService.findPath(findPathParams, function(serviceResult) {
          // 获取服务器返回的结果
          if (serviceResult.result) {
            // map.getSource("pathPlanGeojson").setData(serviceResult.result.pathList[0].route);
            resolve(serviceResult)
          } else {
            reject('路径规划失败！')
          }
        })
      })

      // 向服务器发送请求，并对返回的结果进行分析处理，展示在客户端上
    }
  }

  // 添加路径规划完毕之后的路线图层
  static addPathPlanLayer(map) {
    if (map.getSource('pathPlanGeojson') === undefined) {
      map.addSource('pathPlanGeojson', {
        type: 'geojson',
        data: pathPlanGeojson
      })
    }
    if (map.getLayer('pathPlan-lines') === undefined) {
      map.addLayer({
        id: 'pathPlan-lines',
        type: 'line',
        source: 'pathPlanGeojson',
        layout: {
          'line-cap': 'round',
          'line-join': 'round'
        },
        paint: {
          'line-color': '#459C50',
          'line-width': 5
        },
        filter: ['==', '$type', 'LineString']
      })
    }
  }

  // 路径导航每一段高亮
  static addPathPlanGuidLayer(map) {
    if (map.getSource('pathPlanGuildGeojson') === undefined) {
      map.addSource('pathPlanGuildGeojson', {
        type: 'geojson',
        data: pathPlanGuidGeoJson
      })
    }
    if (map.getLayer('pathPlanGuid-lines') === undefined) {
      map.addLayer({
        id: 'pathPlanGuid-lines',
        type: 'line',
        source: 'pathPlanGuildGeojson',
        layout: {
          'line-cap': 'round',
          'line-join': 'round'
        },
        paint: {
          'line-color': '#FE4C88',
          'line-width': 5
        },
        filter: ['==', '$type', 'LineString']
      })
    }
  }

  // 删除显示规划路径的路线图层
  static removePathPlanLayer(map) {
    if (map.getLayer('pathPlan-lines') !== undefined) {
      map.removeLayer('pathPlan-lines')
    }
    if (map.getLayer('pathPlan-points') !== undefined) {
      map.removeLayer('pathPlan-points')
    }
    if (map.getSource('pathPlanGeojson') !== undefined) {
      map.removeSource('pathPlanGeojson')
    }
  }

  // 删除路径导航每一段高亮图层
  static removePathPlanGuidLayer(map) {
    if (map.getLayer('pathPlanGuid-lines') !== undefined) {
      map.removeLayer('pathPlanGuid-lines')
    }
    if (map.getSource('pathPlanGuildGeojson') !== undefined) {
      map.removeSource('pathPlanGuildGeojson')
    }
  }

  static setPointType(setPointType) {
    pointType = setPointType
  }

  static setStartPoint(lng, lat, map) {
    if (startPointMarker !== null) {
      startPointMarker.remove()
    }
    startPoint = new mapboxgl.LngLat(lng, lat)
    const startMarkerDiv = document.createElement('div')
    startMarkerDiv.className = 'marker'
    startMarkerDiv.style.backgroundImage =
      'url(./static/img/pathPlan/start.png)'
    startMarkerDiv.style.width = 25 + 'px'
    startMarkerDiv.style.height = 37 + 'px'

    startPointMarker = new mapboxgl.Marker(startMarkerDiv, {
        offset: [0, -8]
      })
      .setLngLat([lng, lat])
      .addTo(map)
  }

  static setEndPoint(lng, lat, map) {
    if (endPointMarker !== null) {
      endPointMarker.remove()
    }

    endPoint = new mapboxgl.LngLat(lng, lat)

    const endMarkerDiv = document.createElement('div')
    endMarkerDiv.className = 'marker'
    endMarkerDiv.style.backgroundImage = 'url(./static/img/pathPlan/end.png)'
    endMarkerDiv.style.width = 25 + 'px'
    endMarkerDiv.style.height = 37 + 'px'

    endPointMarker = new mapboxgl.Marker(endMarkerDiv, {
        offset: [0, -8]
      })
      .setLngLat([lng, lat])
      .addTo(map)
  }

  static setPathPlanPoint(map, lng, lat) {
    pathPlanPoints.push(new mapboxgl.LngLat(lng, lat))
    const throughMarkerDiv = document.createElement('div')
    throughMarkerDiv.className = 'marker'
    throughMarkerDiv.style.backgroundImage =
      'url(./static/img/pathPlan/through.png)'
    throughMarkerDiv.style.width = 25 + 'px'
    throughMarkerDiv.style.height = 37 + 'px'

    const throungPointMarker = new mapboxgl.Marker(throughMarkerDiv, {
        offset: [0, -8]
      })
      .setLngLat([lng, lat])
      .addTo(map)
    markers.push(throungPointMarker)
  }

  static changePathPlanPoint(map, lng, lat, index) {
    markers[index].remove()
    pathPlanPoints[index] = new mapboxgl.LngLat(lng, lat)
    const throughMarkerDiv = document.createElement('div')
    throughMarkerDiv.className = 'marker'
    throughMarkerDiv.style.backgroundImage =
      'url(./static/img/pathPlan/through.png)'
    throughMarkerDiv.style.width = 25 + 'px'
    throughMarkerDiv.style.height = 37 + 'px'

    const throungPointMarker = new mapboxgl.Marker(throughMarkerDiv, {
        offset: [0, -8]
      })
      .setLngLat([lng, lat])
      .addTo(map)
    markers[index] = throungPointMarker
  }

  static removePathPlanPoint(index) {
    if (typeof (markers[index]) !== 'undefined') {
      markers[index].remove()
      markers.splice(index, 1)
      pathPlanPoints.splice(index, 1)
    }
  }

  static drawPathPlanPoint(e) {
    switch (pointType) {
      case 0:
        startPoint = new mapboxgl.LngLat(e.lngLat.lng, e.lngLat.lat)
        if (startPointMarker !== null) {
          startPointMarker.remove()
        }

        const startMarkerDiv = document.createElement('div')
        startMarkerDiv.className = 'marker'
        startMarkerDiv.style.backgroundImage =
          'url(./static/img/pathPlan/start.png)'
        startMarkerDiv.style.width = 25 + 'px'
        startMarkerDiv.style.height = 37 + 'px'

        startPointMarker = new mapboxgl.Marker(startMarkerDiv, {
            offset: [0, -8],
            draggable: true
          })
          .setLngLat([e.lngLat.lng, e.lngLat.lat])
          .addTo(e.target)

        startPointMarker.on('dragend', e => {
          startPoint = e.target._lngLat
        })
        break
      case 1:
        pathPlanPoints.push(new mapboxgl.LngLat(e.lngLat.lng, e.lngLat.lat))

        const throughMarkerDiv = document.createElement('div')
        throughMarkerDiv.className = 'marker'
        throughMarkerDiv.style.backgroundImage =
          'url(./static/img/pathPlan/through.png)'
        throughMarkerDiv.style.width = 25 + 'px'
        throughMarkerDiv.style.height = 37 + 'px'

        const throungPointMarker = new mapboxgl.Marker(throughMarkerDiv, {
            offset: [0, -8]
          })
          .setLngLat([e.lngLat.lng, e.lngLat.lat])
          .addTo(e.target)
        markers.push(throungPointMarker)
        break
      case 2:
        break
      case 3:
        endPoint = new mapboxgl.LngLat(e.lngLat.lng, e.lngLat.lat)
        if (endPointMarker !== null) {
          endPointMarker.remove()
        }

        const endMarkerDiv = document.createElement('div')
        endMarkerDiv.className = 'marker'
        endMarkerDiv.style.backgroundImage =
          'url(./static/img/pathPlan/end.png)'
        endMarkerDiv.style.width = 25 + 'px'
        endMarkerDiv.style.height = 37 + 'px'

        endPointMarker = new mapboxgl.Marker(endMarkerDiv, {
            offset: [0, -8],
            draggable: true
          })
          .setLngLat([e.lngLat.lng, e.lngLat.lat])
          .addTo(e.target)

        endPointMarker.on('dragend', e => {
          endPoint = e.target._lngLat
        })
        break
    }
  }

  static clearPathPlan(map) {
    markers.forEach(marker => {
      marker.remove()
    })
    pathPlanGeojson = {
      type: 'FeatureCollection',
      features: []
    }

    if (startPointMarker !== null) {
      startPointMarker.remove()
    }
    if (endPointMarker !== null) {
      endPointMarker.remove()
    }

    map.getSource('pathPlanGeojson').setData(pathPlanGeojson)
    // this.removePathPlanLayer(map);
    pathPlanPoints = []

    startPoint = null

    endPoint = null

    pointType = -1
  }
}

export {
  PathPlan
}
